<?php

class frontController extends manageController {

    public function __construct() {
        if (!isset($_SESSION['zp_manage']['islogin']) || empty($_SESSION['zp_manage']['islogin'])) {
            Tools::AlertTxt('?md=manage&cl=login&at=login', '请先登录');
            exit;
        }
    }

    public function question_listAction() {
        $table = "fqa";
        $func = (!empty($this->params['func'])) ? $this->params['func'] : '';
        $page = (!empty($this->params['page'])) ? (int) $this->params['page'] : 1;
        $number_page = (!empty($this->params['number_page'])) ? $this->params['number_page'] : Data::$_number_page;
        $this->view->setVariable("number_page", $number_page);
        $this->view->setVariable("page", $page);

        $query = (!empty($this->params['query'])) ? $this->params['query'] : '';
        $query = urldecode($query);
        $this->view->setVariable("query", $query);
        $q_permission = (!empty($this->params['q_permission'])) ? $this->params['q_permission'] : '';
        $this->view->setVariable("q_permission", $q_permission);

        //上下線
        if (!empty($func) && $func == "status") {
            $id = empty($this->params["id"]) ? '0' : (int) $this->params["id"];
            if ($id > 0) {
                $where_fields = array("AND" => array("=" => array("id" => $id)));
                $row = $this->model->getItemById($id, $table);
                $field = array();
                $field['status'] = $row['status'] == '1' ? '0' : '1';
                $field['update_id'] = $_SESSION['zp_manage']['uid'];
                $field['updatedt'] = 'now()';
                $r = $this->model->updateItem($field, $where_fields, $table);

                //日志
                $log = array();
                $log['table_id'] = $id;
                $log['table_name'] = $table;
                $log['type'] = 4;
                $log['page'] = "前端常见问题上下线";
                $log['content'] = $log['page'] . "：" . 1;
                $this->model->setLog($log, $row, $field);
            }
           echo json_encode(array('status'=>1),JSON_UNESCAPED_UNICODE);
            exit;
        }

        //刪除
        if (!empty($func) && $func == "del") {
            $id = empty($this->params["id"]) ? '0' : (int) $this->params["id"];
            $field = array("isdeleted" => 1, 'update_id' => $_SESSION['zp_manage']['uid'], 'updatedt' => 'now()');
            $where_fields = array("AND" => array("=" => array("id" => $id)));
            $this->model->updateItem($field, $where_fields, $table);
            $row = $this->model->getItemById($id, $table);

            //日志
            $log = array();
            $log['table_id'] = $id;
            $log['table_name'] = $table;
            $log['type'] = 3;
            $log['page'] = "删除常见问题";
            $log['content'] = $log['page'] . "：" . $this->params["id"];
            $this->model->setLog($log, $row, $field);
            echo json_encode(array('status'=>1),JSON_UNESCAPED_UNICODE);
            exit;
        }

        //抓列表
        $where_fields = array("AND" => array("=" => array("isdeleted" => 0)));
//        if (!empty($q_permission)) {
//            $where_fields['AND']['=']['said'] = $q_permission;
//        }
        $like_fields = array();
//        if (!empty($query)) {
//            $like_fields = array('account' => $query, 'name' => $query);
//        }
        $orderby = array('id' => 'desc');
        $total_count = $this->model->getTotalCount($table, $where_fields, $like_fields);
        $rsp_list = $this->model->getAllByPage($page, $number_page, $where_fields, $table, $like_fields, $orderby);
        $rsp_page_list = Tools::getPagesPost($_SERVER["QUERY_STRING"], $number_page, $page, $total_count);
        $this->view->setVariable("rsp_list", $rsp_list);
        $this->view->setVariable("rsp_page_list", $rsp_page_list);
        $this->view->setVariable("total_count", $total_count);
        $this->view->setVariable("page", $page);
        $this->view->setVariable("number_page", $number_page);
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/fronted/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    public function question_listaddAction() {
        $table = "fqa";
        $page = (!empty($this->params['page'])) ? $this->params['page'] : 1;
        $number_page = (!empty($this->params['number_page'])) ? $this->params['number_page'] : Data::$_number_page;
        $this->view->setVariable("number_page", $number_page);
        $this->view->setVariable("page", $page);
        $id = (!empty($this->params['id'])) ? (int) $this->params['id'] : 0;

        if ($id > 0) {
            //抓單筆資料
            $rsp_list = $this->model->getItemById($id, $table);
            $this->view->setVariable("rsp_list", $rsp_list);
        }
        $this->view->setVariable("number_page", $number_page);
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/fronted/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    public function question_listupdateAction() {
        $table = "fqa";
        $page = (!empty($this->params['page'])) ? $this->params['page'] : 1;
        $id = (!empty($this->params['id'])) ? $this->params['id'] : '';
        if($id){
            $data['title'] = $this->params['title'];
            $data['text'] = $this->params['text'];
            $data['updatedt'] = 'now()';
            $data['update_id'] = $_SESSION['zp_manage']['uid'];
            $where_fields = array("AND" => array("=" => array('id' => $id)));
            $r = $this->model->updateItem($data, $where_fields, $table);
        }else{
            $data['title'] = $this->params['title'];
            $data['text'] = $this->params['text'];
            $data['status'] = 1;
            $data['createdt'] = 'now()';
            $data['create_id'] = $_SESSION['zp_manage']['uid'];
            $r = $this->model->addItem($data, $table);
            if(!$r){
                $vaild_msg= '新增失败';
                Tools::GoToPrev($vaild_msg);
                return;
            }
        }

        $url = '?md=' . MvcReg::$_moduleName . '&cl=' . MvcReg::$_controllerName . '&at=' . str_replace('update', '', MvcReg::$_actionName);
        $url .='&page='.$page;
        Tools::AlertTxt($url, $this->getKeyWord('SAVE') . $this->getKeyWord('Msg_Successful'));
    }


    public function message_centAction() {
        $table = "news";
        $func = (!empty($this->params['func'])) ? $this->params['func'] : '';
        $page = (!empty($this->params['page'])) ? (int) $this->params['page'] : 1;
        $number_page = (!empty($this->params['number_page'])) ? $this->params['number_page'] : Data::$_number_page;
        $this->view->setVariable("number_page", $number_page);
        $this->view->setVariable("page", $page);

        //上下線
        if (!empty($func) && $func == "status") {
            $id = empty($this->params["id"]) ? '0' : (int) $this->params["id"];
            if ($id > 0) {
                $where_fields = array("AND" => array("=" => array("id" => $id)));
                $row = $this->model->getItemById($id, $table);
                $field = array();
                $field['status'] = $row['status'] == '1' ? '0' : '1';
                $field['update_id'] = $_SESSION['zp_manage']['uid'];
                $field['updatedt'] = 'now()';
                $r = $this->model->updateItem($field, $where_fields, $table);

                //日志
                $log = array();
                $log['table_id'] = $id;
                $log['table_name'] = $table;
                $log['type'] = 4;
                $log['page'] = "消息上下线";
                $log['content'] = $log['page'] . "：" . 1;
                $org = array();
                $field1 = array();
                $this->model->setLog($log, $row, $field);
            }
            echo json_encode(array('status'=>1),JSON_UNESCAPED_UNICODE);
            exit;
        }

        //刪除
        if (!empty($func) && $func == "del") {
            $id = empty($this->params["id"]) ? '0' : (int) $this->params["id"];
            $field = array("isdeleted" => 1, 'update_id' => $_SESSION['zp_manage']['uid'], 'updatedt' => 'now()');
            $where_fields = array("AND" => array("=" => array("id" => $id)));
            $this->model->updateItem($field, $where_fields, $table);
            $row = $this->model->getItemById($id, $table);

            //日志
            $log = array();
            $log['table_id'] = $id;
            $log['table_name'] = $table;
            $log['type'] = 3;
            $log['page'] = "删除消息";
            $log['content'] = $log['page'] . "：" . $this->params["id"];
            $this->model->setLog($log, $row, $field);
            echo json_encode(array('status'=>1),JSON_UNESCAPED_UNICODE);
            exit;
        }

        //抓列表
        $where_fields = array("AND" => array("=" => array("isdeleted" => 0)));
        $like_fields = array();
        $orderby = array('id' => 'desc');
        $total_count = $this->model->getTotalCount($table, $where_fields, $like_fields);
        $rsp_list = $this->model->getAllByPage($page, $number_page, $where_fields, $table, $like_fields, $orderby);
        $rsp_page_list = Tools::getPagesPost($_SERVER["QUERY_STRING"], $number_page, $page, $total_count);

        $this->view->setVariable("rsp_list", $rsp_list);
        $this->view->setVariable("rsp_page_list", $rsp_page_list);
        $this->view->setVariable("total_count", $total_count);
        $this->view->setVariable("page", $page);
        $this->view->setVariable("number_page", $number_page);
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/fronted/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }


    public function message_centaddAction() {
        $table = "news";
        $page = (!empty($this->params['page'])) ? $this->params['page'] : 1;
        $number_page = (!empty($this->params['number_page'])) ? $this->params['number_page'] : Data::$_number_page;
        $this->view->setVariable("number_page", $number_page);
        $this->view->setVariable("page", $page);
        $id = (!empty($this->params['id'])) ? (int) $this->params['id'] : 0;

        if ($id > 0) {
            //抓單筆資料
            $rsp_list = $this->model->getItemById($id, $table);
            $this->view->setVariable("rsp_list", $rsp_list);
        }
        $this->view->setVariable("number_page", $number_page);
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/fronted/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }

    public function message_centupdateAction() {
        $table = "news";
        $page = (!empty($this->params['page'])) ? $this->params['page'] : 1;
        $id = (!empty($this->params['id'])) ? $this->params['id'] : '';
        if($id){
            $data['title'] = $this->params['title'];
            $data['text'] = $this->params['text'];
            $data['updatedt'] = 'now()';
            $data['update_id'] = $_SESSION['zp_manage']['uid'];
            $where_fields = array("AND" => array("=" => array('id' => $id)));
            $r = $this->model->updateItem($data, $where_fields, $table);
        }else{
            $data['title'] = $this->params['title'];
            $data['text'] = $this->params['text'];
            $data['status'] = 9;
            $data['ico'] = 1;
            $data['createdt'] = 'now()';
            $data['create_id'] = $_SESSION['zp_manage']['uid'];
            $r = $this->model->addItem($data, $table);
            if(!$r){
                $vaild_msg= '新增失败';
                Tools::GoToPrev($vaild_msg);
                return;
            }
        }

        $url = '?md=' . MvcReg::$_moduleName . '&cl=' . MvcReg::$_controllerName . '&at=' . str_replace('update', '', MvcReg::$_actionName);
        $url .='&page='.$page;
        Tools::AlertTxt($url, $this->getKeyWord('SAVE') . $this->getKeyWord('Msg_Successful'));
    }


}

?>
